<?php

class MemberBatchAdd extends BatchAdd{

	//第一行的正确格式
	public $headers 	= null;
	//正确数据项数量
	public $item_count 	= null;
	//检查的错误
	public $format_errors	= null;
	//空行数量
	public $empey_count		= null;
	//空行数量
	public $right_lines		= null;
	
	function __construct(){
		parent::__construct();

		//第一行的正确格式
		$this->headers = array(
			'用户名称',
			'密码',
			'支付密码',
			'网站平台编号',
			'备注'
		);
		
		$this->item_count = count($this->headers);
		
		$this->empey_count = 0;
	}
	
	//检查格式
	function format_check(){
		//执行此方法前要先执行analyze
		
		if($this->line_count > 1){
			
			//第一行
			$first_line = array_shift($this->lines);
			
			//检查格式是否正确
			$first_check = $this->first_line_check($first_line);
			
			//首行正确
			if($first_check){

				foreach($this->lines as $i => $line){
					$this->line_check($line,$i);	
				}
				
				if(count($this->format_errors) > 0){
					//有错误的行
					return false;
				}else{
					return true;
				}
				
			}else{
				
				$this->format_errors[] = '第一行格式不正确';
				return false;
			}
			
			return true;
		}else{
			$this->format_errors[] = '除第一行，至少有一行数据';
			return false;
		}
	}
	
	//分析
	function analyze($content){
		
		return parent::analyze($content);
		
	}
	
	//检查第一行是否正确
	function first_line_check($line){
		
		//去空格,tab
		$line = parent::line_format($line);
				
		if($line != $this->headers){
			$error = '首行格式不正确，请参照格式说明';
			$this->format_errors[] = $error;
			$this->error = $error;
			return false;
		}else{
			return true;	
		}
		
	}
	
	//检查数据行是否正确
	function line_check($line,$line_num){
		
		//去空格,tab
		$items = parent::line_format(trim($line));
		
		if(is_null($items)){
			$this->empey_count++;
			//空数据跳过
			return null;
		}
		
		//数据项数量
		$count = count($items); 
		
		if($this->item_count != $count){
			$this->format_errors[] = ($line_num + 1).'行:'.$line.' 数据项目不正确！应为:'.$this->item_count.'项,实为:'.$count;
			return false;
		}else{
			$this->right_lines[]	= $items;
			return true;
		}
	}
	
	//对数据进行格式化
	function line_format(){
		
		$data = array();		
		
		foreach($this->right_lines as $i => $l){
			$data[] = array(
				'member_name'	=> $l[0],
				'member_pwd'	=> $l[1],
				'member_pay_pwd'=> $l[2],
				'plantform_id'	=> $l[3],
				'remark'		=> $l[4]
			);	
		}
		
		return $data;
	}
}

?>